package com.icoo.apigateway.login.controller;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.crypto.SecureUtil;
import com.icoo.apigateway.system.rabbitMqConfig.RabbitConfig;
import com.icoo.apigateway.user.model.LoginDto;
import com.icoo.apigateway.user.model.Tuser;
import com.icoo.apigateway.user.service.TuserService;
import com.icoo.base.enums.CustomerEnum;
import com.icoo.base.model.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping
@Api("用户登录注册")
public class LoginController {

    @Autowired
    private TuserService loginService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public ResultModel login(@Valid LoginDto loginDto){
        String password = SecureUtil.md5(loginDto.getUserPassword());
        Integer userId = loginService.login(loginDto.getUserName(), password, loginDto.getCustomerSchema());
        if(userId != null){
            StpUtil.login(userId);
            StpUtil.getSession().set(CustomerEnum.CUSTOMER, loginDto.getCustomerSchema());
            /*rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_A, RabbitConfig.ROUTINGKEY_A, 20210908L,new MessagePostProcessor() {
                @Override
                public Message postProcessMessage(Message message) throws AmqpException {
                    message.getMessageProperties().setExpiration("30000");
                    return message;
                }
            });*/
            return ResultModel.success("登录成功！");
        }

        return ResultModel.fail("账号密码错误!");
    }

    @PostMapping("/signUp")
    @Operation(summary = "用户注册")
    public ResultModel signUp(@Valid LoginDto loginDto){
        if(loginService.isExistsName(loginDto.getUserName(), loginDto.getCustomerSchema()))
            return ResultModel.fail("该用户名已存在！");

        loginDto.setUserPassword(SecureUtil.md5(loginDto.getUserPassword()));
        if(loginService.signUp(loginDto) >= 1)
            return ResultModel.success("注册成功!");

        return ResultModel.fail("注册失败,请重试!");
    }


    @GetMapping("/checkLogin")
    @Operation(summary = "验证是否登录")
    public ResultModel checkLogin(){
        try {
            StpUtil.checkLogin();
        }catch (NotLoginException ex){
            return ResultModel.fail("未登录!");
        }

        return ResultModel.success("已登录!");
    }

    @GetMapping("/logout")
    @Operation(summary = "注销登录")
    public ResultModel logout(){
        StpUtil.logout();
        return ResultModel.success("已注销!");
    }
}
